from rest_framework import serializers
from .models import Gathering


# 活动列表
class ActivityModelSerializer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Gathering
        fields = ['id', 'name', 'image', 'city', 'starttime', 'endrolltime', 'users']
        # format为序列化、input_format为反序列化
        extra_kwargs = {
            'starttime': {'format': '%Y-%m-%d'},  # 时间格式
            'endrolltime': {'format': '%Y-%m-%d'},  # 时间格式
        }


# 活动详情
class ActivityDetails(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Gathering
        fields = '__all__'
        extra_kwargs = {
            'starttime': {'format': '%Y-%m-%d'},
            'endtime': {'format': '%Y-%m-%d'},
            'endrolltime': {'format': '%Y-%m-%d'}
        }
